package cn.edu.ujn.booking.controller;

import java.io.File;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.ujn.booking.dao.Dict;
import cn.edu.ujn.booking.dao.Drama;
import cn.edu.ujn.booking.dao.User;
import cn.edu.ujn.booking.service.DictService;
import cn.edu.ujn.booking.service.DramaService;

@Controller
public class DramaController {
	@Autowired
	private DramaService dramaService;
	@Autowired
	private DictService dictService;
	@RequestMapping(value ="/first/list.action")
	public String toFirst(Model model,HttpServletRequest request) {
		List<Drama> all = dramaService.findAll();
		model.addAttribute("dramas",all);
		System.out.println(all);
		return "first";
	}
	@RequestMapping(value="/dramaAdd",method = RequestMethod.POST)
	public String toadd(Drama drama,List<MultipartFile> imageFiles,HttpServletRequest request,HttpSession session) {
		System.out.println(drama);
		String img="";
		if(!imageFiles.isEmpty()&&imageFiles.size()>0) {
			for(MultipartFile file:imageFiles) {
				String originalFilename = file.getOriginalFilename();
				String dirPath = request.getServletContext().getRealPath("/upload/");
				System.out.println(dirPath);
				File filePath = new File(dirPath);
				if(!filePath.exists()) {
					filePath.mkdirs();
				}
				String newFilename=UUID.randomUUID()+"_"+originalFilename;
				
				try {
					file.transferTo(new File(dirPath+newFilename));
					img="upload/"+newFilename;
				}catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		}
		drama.setImg(img);
		int createDrama = dramaService.createDrama(drama);
		System.out.println(drama.getId());
		/*
		 * 插入座位
		 */
		Dict dict = new Dict();
		for(int i=0;i<10;i++) {
			for(int j=0;j<10;j++) {
				dict.setDramas_id(drama.getId());
				dict.setSeat(i+","+j);
				int dict2 = dictService.createDict(dict);
			}
		}
		List<Drama> all = dramaService.findAll();
		session.setAttribute("dramas",all);
		return "redirect:backstage";
	}
	@RequestMapping("/first/delete.action")
	@ResponseBody
	public String toDel(int id,HttpSession session) {
		System.out.println(id);
		int rows = dramaService.deleteByPrimaryKey(id);
		List<Drama> all = dramaService.findAll();
		session.setAttribute("dramas",all);
		if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	@RequestMapping("/second")
	public String toAddTest() {
		return "second";
	}
	@RequestMapping("/first/getDramaById.action")
	@ResponseBody
	public Drama getDramaById(String id) {
		System.out.println(id);
		Drama drama = dramaService.getDramaById(Integer.parseInt(id));
		System.out.println(drama);
		return drama;
	}
	@RequestMapping("/first/update.action")
	public String dramaUpdate(Drama drama,HttpSession session) {
		System.out.println(drama);
		int updateDrama = dramaService.updateDrama(drama);
		System.out.println(updateDrama);
		List<Drama> all = dramaService.findAll();
		session.setAttribute("dramas",all);
		if(updateDrama >0) {
			return "OK";
		}else {
			return "FAIL";
		}
	}
	@RequestMapping("/select.action")
	public String toSelect(String research,HttpSession session) {
		System.out.println(research);
		if(research==null || research=="") {
			session.setAttribute("user_dramas", dramaService.findAll());
		}else {
			List<Drama>list = dramaService.selectDramasByName(research);
			if(list.size()>0) {
				session.setAttribute("user_dramas", list);
				//System.out.println(list);
			}else {
				session.setAttribute("user_dramas", dramaService.findAll());
				
			}
		}
		return "home";
		
	}
	@RequestMapping("/private")
	public String toPri(HttpSession session) {
		User attribute = (User) session.getAttribute("USER_SESSION");
		int u_id = attribute.getId();
		List<Dict> list = dictService.selectByUserIdAndDramaId(u_id);
		session.setAttribute("user_info", list);
		return "private";
	}
	
}
